
/*
Author:Eric Yang
Email:wind6266@163.com
*/
// whether the mouse is on the menu(include dynamic menu)
var mouseOnMenu = false;
// the selected li item.
var selectedItem;
$(document).ready(function () {
	var dynamicMenu = $("#dynamic_menu");
	dynamicMenu.hover(function () {
		mouseOnMenu = true;
		dynamicMenu.show();
		if (selectedItem) {
			selectedItem.addClass("selected");
		}
	}, function () {
		mouseOnMenu = false;
		dynamicMenu.hide();
		if (selectedItem) {
			selectedItem.removeClass("selected");
		}
	});
	/*
	listening the mouseout event with 300ms interval.
	*/
	$("#menu").mouseout(function () {
		setTimeout(function () {
			if (!mouseOnMenu && selectedItem) {
				selectedItem.removeClass("selected");
			}
		}, 300);
	});
	$("#menu > div > ul > li").hover(function () {
		mouseOnMenu = true;
		var li = $(this);
		if (selectedItem) {
			selectedItem.removeClass("selected");
		}
		selectedItem = li;
		selectedItem.addClass("selected");
		var id = li.attr("id");
		var position = li.position();
		dynamicMenu.css("top", position.top);
		dynamicMenu.css("left", position.left + li.width() - 5);
		var container = $("#" + id + "_dynamic");
		if (container.length > 0) {
			var html = container.html();
			dynamicMenu.html(html);
			dynamicMenu.show();
		}
	}, function () {
		mouseOnMenu = false;
		setTimeout(function () {
			if (!mouseOnMenu) {
				dynamicMenu.hide();
			}
		}, 300);
	});
});

